import { BrowserWindow } from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";

const base_url = {
	development: "http://localhost:7778#",
	production: "app://./index.html#",
};

const createWindow = (config, path, title, is_external_page, src) => {
	let options = {
		width: 800, // 窗口宽度
		height: 600, // 窗口高度
		frame: false, // 无边框窗口
		resizable: true, // 禁止调整窗口大小
		transparent: false, // 窗口透明
		backgroundColor: "#fff", // 窗口背景色
		icon: "./src/assets/img/favicon.ico", // 设置图标路径
		webPreferences: {
			nodeIntegration: true, // 启用 Node.js 集成
			contextIsolation: false, // 禁用上下文隔离
			webviewTag: true,
		},
		...config,
	};
	// 在应用程序准备就绪之前注册自定义协议
	createProtocol("app");
	let full_path = `${base_url[process.env.NODE_ENV]}${path}`;
	const win = new BrowserWindow(options);
	// 如果是开发环境,就打开控制台
	// if (process.env.WEBPACK_DEV_SERVER_URL) {
	win.webContents.openDevTools();
	// }
	win.on("maximize", () => {
		win.webContents.send("window-maximized", true);
	});

	win.on("unmaximize", () => {
		win.webContents.send("window-maximized", false);
	});

	win.webContents.on("did-finish-load", () => {
		let param;
		if (is_external_page) {
			param = {
				title,
				src
			}
		}else{
			param = {
				title
			}
		}
		win.webContents.send("send_param", param); // 窗口准备就绪后，发送标题
		// win.show(); // 显示窗口
	});
	win.loadURL(full_path);
	return {
		win: win, // 窗口对象
	};
};

export { createWindow };
